﻿using AME.WebAppCore;
using Microsoft.Extensions.DependencyInjection;
using Model;
using System.Linq.Expressions;
using AME.Repository;


namespace Service
{
    [AutoDI(ServiceLifetime.Scoped)]
    public class UserService
    {
        private readonly IDiffLogRepository<UserModel> _userRepository;

        // 通过依赖注入获取 IRepository 实现
        public UserService(IDiffLogRepository<UserModel> userRepository)
        {
            _userRepository = userRepository;
        }

        // 添加单个用户
        public async Task<bool> AddUserAsync(UserModel user)
        {
            return await _userRepository.AddAsync(user);
        }

        // 批量添加用户
        public async Task<bool> AddUsersAsync(List<UserModel> users)
        {
            return await _userRepository.AddRangeAsync(users);
        }

        // 更新用户
        public async Task<bool> UpdateUserAsync(UserModel user)
        {
            return await _userRepository.UpdateAsync(user);
        }

        // 根据 ID 删除用户
        public async Task<bool> DeleteUserByIdAsync(int id)
        {
            return await _userRepository.DeleteByIdAsync(id);
        }

        // 根据条件删除用户
        public async Task<bool> DeleteUsersAsync(Expression<Func<UserModel, bool>> whereExpression)
        {
            return await _userRepository.DeleteAsync(whereExpression);
        }

        // 根据 ID 获取用户
        public async Task<UserModel> GetUserByIdAsync(int id)
        {
            return await _userRepository.GetByIdAsync(id);
        }

        // 根据条件获取用户列表
        public async Task<List<UserModel>> GetUsersByConditionAsync(Expression<Func<UserModel, bool>> whereExpression)
        {
            return await _userRepository.GetListAsync(whereExpression);
        }

        // 获取所有用户
        public async Task<List<UserModel>> GetAllUsersAsync()
        {
            return await _userRepository.GetAllAsync();
        }

        // 根据条件判断是否存在用户
        public async Task<bool> AnyUserAsync(Expression<Func<UserModel, bool>> whereExpression)
        {
            return await _userRepository.AnyAsync(whereExpression);
        }

        // 获取单个用户（根据条件）
        public async Task<UserModel> GetSingleUserAsync(Expression<Func<UserModel, bool>> whereExpression)
        {
            return await _userRepository.GetSingleAsync(whereExpression);
        }

        // 分页获取用户列表
        public async Task<(List<UserModel> Users, int TotalCount)> GetPagedUsersAsync(UserModel model, int pageIndex, int pageSize)
        {
            // 构建查询条件，根据 isActive 和 name 过滤用户
            Expression<Func<UserModel, bool>> condition = user => user.IsActive == model.IsActive 
                                                            && user.UserName.Contains(model.UserName);

            // 如果提供了 name 参数，则将其添加到查询条件中
            //if (!string.IsNullOrEmpty(model.Name))
            //{
            //    condition = condition.And(condition, condition2);
            //}

            return await _userRepository.GetPagedListAsync(condition, pageIndex, pageSize);
        }
    }
}
